package com.quarters.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quarters.bean.Article;
import com.quarters.bean.ArticleOrder;
import com.quarters.bean.User;
import com.quarters.bean.layuiBean.ResultPage;
import com.quarters.bean.layuiBean.VoPage;
import com.quarters.commen.BaseController;
import com.quarters.util.OrderIdUtil;
import com.quarters.util.Respons.ResponseUtil;
import com.quarters.util.exception.CommonException;
import com.sun.org.apache.regexp.internal.RE;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 文章表 前端控制器
 * </p>
 *
 * @author 冯政尚
 * @since 2019-09-03
 */
@Controller
@RequestMapping("admin/article")
public class ArticleController extends BaseController {


    @RequestMapping(value = "article_add.html")
    public String article_add(){
        return "admin/article/article_add";
    }
    @RequestMapping(value = "article_edit.html")
    public String article_edit(Model model,Long id){
        Article article = articleService.getById(id);
        model.addAttribute("article",article);
      String  content = article.getContent().replaceAll("(\r\n|\r|\n|\n\r)", "<br>");
//        String content = delHtmlTag(article.getContent());
         model.addAttribute("content",content);
        return "admin/article/article_edit";
    }

    /**
     * @param str
     * @return 去除html标签
     */
    public static String delHtmlTag(String str){
        String newstr = "";
        newstr = str.replaceAll("<[.[^>]]*>","");
        newstr = newstr.replaceAll(" ", "");
        return newstr;
    }

    /**
     * @param article
     * @return 删除
     */
    @RequestMapping(value = "deleteByIdArticle")
    @ResponseBody
    public Map deleteByIdArticle(@RequestBody Article article){
         if(articleService.removeById(article)){
             QueryWrapper queryWrapper = new QueryWrapper();
             queryWrapper.eq("article_id",article.getId());
             ArticleOrder articleOrder = articleOrderService.getOne(queryWrapper);
             if(articleOrderService.removeById(articleOrder)){
                 return ResponseUtil.getSuccessMap();
             }
         }
        return ResponseUtil.getExceptionMap();
    }


    /**
     * @param voPage
     * @return 后台查询
     */
    @RequestMapping(value = "selectMyPage",method = RequestMethod.GET)
    @ResponseBody
    public ResultPage<Article> selectMyPage(VoPage voPage,HttpSession session ){
        if(getUserInfo(session).getUsername().equals("admin")){
            return articleService.selectMyPageAll(voPage);
        }
        return articleService.selectMyPage(voPage,getUserInfo(session).getId());
    }


    /**
     * @param article
     * @return 添加
     */
    @RequestMapping(value = "saveArticle",method = RequestMethod.POST)
    @ResponseBody
    public Map saveArticle(@RequestBody Article article,HttpSession session){
        article.setCreateTime(System.currentTimeMillis());
        article.setUserInfoId(getUserInfo(session).getId());
        article.setType(4);
        if(articleService.save(article)){
            return ResponseUtil.getSuccessMap();
        }
        return ResponseUtil.getExceptionMap();
    }


    /**
     * @param userId
     * @return 发送文章
     */
    @RequestMapping(value = "sendArticles",method = RequestMethod.POST)
    @ResponseBody
    public void sendArticles(
            String userId, Long articleId, String title, String content, HttpSession session,Long createTime){
            String userIds[] = userId.split(",");
        ArticleOrder articleOrder = new ArticleOrder();
        articleOrder.setTitle(title);
        articleOrder.setContent(content);
        articleOrder.setOrderNumber(OrderIdUtil.gen("CZ",1L));
        articleOrder.setCreateTime(System.currentTimeMillis());
        articleOrder.setUserList(userId);
        articleOrder.setArticleId(articleId);
        articleOrder.setTypeType(3);
        articleOrder.setFlage(2);
        articleOrder.setType(1);
        articleOrder.setUserId(getUserInfo(session).getId());
        //如果是超级管理员直接发送
            if(getUserInfo(session).getUsername().equals("admin")){
                if(articleOrderService.save(articleOrder)){
                    Article article = articleService.getById(articleId);
                    article.setType(1);
                    if(articleService.updateById(article)){
                        //给区域管理员发送模板消息
//                        articleService.examine(getUserInfo(session).getId(),System.currentTimeMillis(),articleOrder.getOrderNumber());
                        //是超级管理员 酒直接发送给用户  用户集合  文章id 标题
                        articleService.send(userId,title,articleId,createTime);

                        throw new CommonException("推送成功!");
                    }else{
                        throw new CommonException("修改状态失败!");
                    }
                }else{
                    throw new CommonException("推送失败");
                }
            }
            //否则审核支付
            if(articleOrderService.save(articleOrder)){
                Article article = articleService.getById(articleId);
                article.setType(3);
                if(articleService.updateById(article)){
                    //给区域管理员发送模板消息
                    articleService.examine(getUserInfo(session).getId(),System.currentTimeMillis(),articleOrder.getOrderNumber());
                    throw new CommonException("提交成功,管理员进行审核!");
                }else{
                    throw new CommonException("修改状态失败!");
                }
            }else{
                throw new CommonException("提交失败");
            }
    }





}
